package com.app.mingluxing.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.mingluxing.R;
import com.app.mingluxing.bean.MessageBean;
import com.app.mingluxing.utils.AbDateUtil;
import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

/**
 * 历史聊天联系人页面adapter
 * Created by Aaron on 15/12/21.
 */
public class ChatListAdapter extends BaseAdapter {
    //    private LayoutInflater mLayoutInflater;
    private Activity mContext;
    private List<MessageBean> chatLists;
    private SharedPreferences sp;

    public ChatListAdapter(Activity context, List<MessageBean> topics) {

//        if (context==null)
//            throw new Exception(" context 为 空 了 !!!!! ");

        this.mContext = context;
//        mLayoutInflater = LayoutInflater.from(context);
        this.chatLists = topics;
        sp = context.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
    }

    public void setData(List<MessageBean> topics) {
        this.chatLists = topics;
    }

//    //创建新View，被LayoutManager所调用
//    @Override
//    public ChatListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        return new ChatListViewHolder(mLayoutInflater.inflate(R.layout.item_chat_list, parent, false));
//    }

//    //将数据与界面进行绑定的操作
//    @Override
//    public void onBindViewHolder(ChatListViewHolder holder, int position) {
//
//        //设置聊天条目数据
//        setChatItemData(holder, position);
//
//    }

    /**
     * 设置聊天条目数据
     *
     * @param holder
     * @param position
     */
    private void setChatItemData(ViewHolder holder, int position) {

        if (chatLists == null)
            return;

        //未读消息个数
        int unReadCount = chatLists.get(position).isRead;
        if (unReadCount > 99) unReadCount = 99;
        holder.tv_count.setText(unReadCount + "");
        holder.tv_count.setVisibility(unReadCount > 0 ? View.VISIBLE : View.INVISIBLE);

        holder.tv_content.setTextColor(mContext.getResources().getColor(R.color.textColor_9b));

        switch (chatLists.get(position).messageType) {//内容

            case Constant.MQTTMESSAGETEXT://文字
            case Constant.MQTTMESSAGENOTICE:// 创建群 增删群成员

                holder.tv_content.setText(chatLists.get(position).content);

                break;

            case Constant.MQTTMESSAGEPHOTO://图片

                holder.tv_content.setText("[图片]");

                break;

            case Constant.MQTTMESSAGEVOICE://语音

                holder.tv_content.setText("[语音]");

                holder.tv_content.setTextColor(
                        mContext.getResources().getColor(
                                chatLists.get(position).isListener == Constant.UNLISTENER ?
                                        R.color.red : R.color.textColor_9b)
                );

                break;

            case Constant.MQTTMESSAGETODO://代办

                holder.tv_content.setText("[代办]");

                break;

        }

        //发送时间
        holder.tv_time.setText(AbDateUtil.getChatDate(chatLists.get(position).sendDate));

        //判断是否是群聊
        if (CommonUtils.isGroupChat(chatLists.get(position).channelId)) {
            //群聊
            holder.tv_title.setText(chatLists.get(position).receiveName);//聊天名称

            Glide.with(mContext)
                    .load(chatLists.get(position).receivePic)
                    .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                    .placeholder(R.mipmap.temp_pic)
                    .into(holder.iv_headpic);//聊天头像

        } else {
            //私聊

            //判断发送者是否是本人
            if (sp.getString(Constant.USERID, "").equals(chatLists.get(position).sendId)) {

                //是本人发送的
                holder.tv_title.setText(chatLists.get(position).receiveName);//聊天对象的昵称

//                LogUtil.e("tag", "聊天对象的昵称 和 头像 : " + chatLists.get(position).receiveName + "," + chatLists.get(position).receivePic);

                Glide.with(mContext)
                        .load(chatLists.get(position).receivePic)
                        .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                        .placeholder(R.mipmap.temp_pic)
                        .into(holder.iv_headpic);//聊天头像

            } else {

                //不是本人发送的
                holder.tv_title.setText(chatLists.get(position).sendName);//聊天对象的昵称

//                LogUtil.e("tag", "聊天对象的昵称 和 头像 : " + chatLists.get(position).sendName + "," + chatLists.get(position).sendPic);

                Glide.with(mContext)
                        .load(chatLists.get(position).sendPic)
                        .dontAnimate().diskCacheStrategy(DiskCacheStrategy.SOURCE).fitCenter()
                        .placeholder(R.mipmap.temp_pic)
                        .into(holder.iv_headpic);//聊天头像

            }

        }
    }

//    //获取数据的数量
//    @Override
//    public int getItemCount() {
//        return chatLists.size();
//    }

    @Override
    public int getCount() {

        if (chatLists == null)
            return 0;

        return chatLists.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            convertView = View.inflate(mContext, R.layout.item_chat_list, null);
            viewHolder = new ViewHolder();

//            iv_notify = (ImageView) view.findViewById(R.id.iv_notify);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
//            tv_newcount = (TextView) view.findViewById(R.id.tv_new_count);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
//            tv_agree = (TextView) view.findViewById(R.id.tv_agree);
            viewHolder.iv_headpic = (ImageView) convertView.findViewById(R.id.iv_head_pic);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        setChatItemData(viewHolder, position);

        return convertView;
    }

    private static class ViewHolder {
        TextView tv_content;
        //        TextView tv_newcount;
        TextView tv_time;
        TextView tv_count;
        TextView tv_title;
        //        TextView tv_agree;
        ImageView iv_headpic;
    }

}
